import { createApp } from 'vue'
import { initRouter } from '@/router';
import {initStore, useAuthStore} from './store'
import '@/styles/index.scss';
import App from './App.vue';
import "virtual:uno.css";

async function bootstrapApp() {
  const app = createApp(App);
  initStore(app);
  initRouter(app);
  // 解析 Token
  const authStore = useAuthStore();
  try {
    const iframeSrc = window?.location?.href;
    if (iframeSrc) {
      const urlObj = new URL(iframeSrc);
      const hashQuery = urlObj.hash.split('?')[1];
      const params = new URLSearchParams(hashQuery);
      const accessToken = params.get('accessToken');
      if (accessToken) {
        authStore.parseToken(accessToken); // 更新全局状态
      }
    }
  } catch (error) {
    console.error('消息处理失败:', error);
  }finally {
    // const urlObj = new URL("http://198.87.103.134/chat/#/chat?name=ai&monitor_name=OCS%E7%B3%BB%E7%BB%9F500KV220kV%E6%B8%94%E9%98%B3%E5%8F%98%E7%94%B5%E7%AB%99_2#%E4%B8%BB%E5%8F%98220%E6%95%B0%E6%8D%AE%E4%B8%8D%E5%88%B7%E6%96%B0&pathName=/console/embed/outer/YWlA5pm66IO95Yqp5omL&accessToken=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkY2FjZmIxYWE3NDY0YjU1OTQ5MTA4MDdiYjg4N2QyZSIsInZlciI6IkRCMS4wIiwidXNlcl9uYW1lIjoiYXlvbmciLCJwcmkiOlsiQURNSU4iXSwibGFzdF9sb2dpbiI6MTc1MTI3NjUyOSwic2NvcGUiOlsiYWxsIl0sImlzcyI6Im5yZWMiLCJleHAiOjE3NTEzNjI5MjksImp0aSI6ImY4N2U3NzcyLTJiZGYtNDgwMi05YmJmLWU5MzE5ZWNmODNhNyIsImNsaWVudF9pZCI6ImZyb250ZW5kIn0.Qdv7SHZ8gEYeDwBO2GC6iFy-6ejKBrhrGFc2t9qr-pnyMKy-K5_3rsUql25JzQ0D0enYAlydp4FM-lNqW7NuVaYEXARFOkWOoequOmEhHa5UygVffN-MBR1rWEDHT85tM7Kizn4PuxU_vWVCaJNGVmdxJVpIY_QejodGpIOVjrI");
    // const hashQuery = urlObj.hash.split('?')[1];
    // const params = new URLSearchParams(hashQuery);
    // const accessToken = params.get('accessToken');
    // if (accessToken) {
    //   authStore.parseToken(accessToken); // 更新全局状态
    // }
    //打印用户信息
    console.log("登录用户是：", authStore.getUser)
  }
  app.mount('#app');
}
bootstrapApp();
